import FlexSearch from "flexsearch";
import { load, loadDict, extract, cut } from "@node-rs/jieba";
import { readFileSync } from "fs";
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

console.log("__dirname", __dirname);
console.log("__filename", __filename);

const buffer = readFileSync(`${__dirname}/userdict.utf8`);
loadDict(buffer);

console.log(cut("中南林业科技大学是个不错的学校"));

// console.log("中南林业科技大学是个不错的学校".replace(/[\x00-\x7F]/g, ""));
// console.log(
//   "中南林业科技大学是个不错的学校".replace(/[\x00-\x7F]/g, "").split("")
// );

const index = new FlexSearch.Index({
  // stemmer: {
  //   中南林业科技大学: "林科大",
  // },
  filter: (words) => {
    console.log("filter words:", words);
    return false;
  },
  // encode: (str) => cut(str),
});

index.add(0, "中南林业科技大学是个不错的学校啊");
index.add(1, "上海交大是个不错的学校");

// index.export((key, data) => {
//   console.log("导出:", key, data);
// });
console.log("`学校`搜索结果", index.search("学校"));
console.log("`中南`搜索结果", index.search("中南"));
console.log("`中南林业科技大学`搜索结果", index.search("中南林业科技大学"));
console.log("`林科大`搜索结果", index.search("林科大"));
console.log("`林大`搜索结果", index.search("林大"));
